use strict;
use warnings;

my $WS = " 	\n";

sub read_file {

	my $filename = shift;
	#printf("reading file: $filename\n");

	my $game_data = [];
	my $arrays = [];
	my $current_array;
	push @$arrays, $game_data;
	$current_array = $arrays->[@$arrays-1];

	open(my $in, "<", $filename) or die("$filename: $!\n");

	#printf("contents:\n");
	while (<$in>) {
		#printf("$_");

		while (length > 0) {

			s/^[$WS]*//;

			if (/^-?[0-9]+\.?[0-9]*/) {
				s/^(-?[0-9]+\.?[0-9]*)//;
				#printf("found number: $1\n");
				push @$current_array, ["\$", $1];
			}
			elsif (/^[a-zA-Z~]+/) {
				s/^([a-zA-Z]+)//;
				#printf("found string: $1\n");
				push @$current_array, ["^", $1];
			}
			elsif (/^\[/) {
				s/^\[//;
				#printf("found new array\n");
				my $new_array = [];
				push @$arrays, $new_array;
				push @$current_array, $new_array;
				$current_array = $new_array;
			}
			elsif (/^\]/) {
				s/^\]//;
				#printf("closing array\n");
				pop @$arrays;
				$current_array = $arrays->[@$arrays-1];
			}
			# remove comments
			elsif (/^#/) {
				s/^(#.*$)//;
				#printf("found comment: $1\n");
			}
			elsif (length > 0) {
				my $sub_name = (caller(0))[3];
				printf("$0 > $sub_name > error reading '$filename' > found element that can't be parsed\n");
				printf("remainter: $_\n");
				return 0;
			}

		}
	}
	#printf("done parsing\n");


	#printf("returning: $game_data\n");
	return $game_data;

} # read file

return 1;
